package com.jysz.certificate.persistence.model.pm;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.mf.BillOption;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;
import com.jysz.webcore.utils.ReflectionUtils;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Entity
@Table(name = "pm_pa_shop_month_detail")
@org.hibernate.annotations.Entity
public class PAShopMonthDetail extends AbstractBaseEntity<Long>  {

	/**
	 * 
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PerformanceAssignShopDetail performanceAssignShopDetail;
	private double payinAmount; //实收管理费金额
	private double totalAmount; //驻店员绩效总额
	private double areaTotalAmount; //区域经理绩效总额
	private PmUserList pmUserList;
	private Integer currenMonthInQty;//当月入库数
	private Integer preMonthInQty;//上月结存数
	
	/** 备注*/
	private String remark;
	
	/** 费用明细*/
	private List<BillOption> billOptions;
	
	private List<AssignUser> assignUsers; //相关人员分配记录
	
	private String monthDate;//绩效月份,几月份的绩效 
	
//	private List<UserPerformance> userPerformances;
	
	@Column(name = "_total_amount")
	public double getTotalAmount() {
		return totalAmount;
	}
	
	@Column(name = "_payin_amount")
	public double getPayinAmount() {
		return payinAmount;
	}
	
	public void setPayinAmount(double payinAmount) {
		this.payinAmount = payinAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

	@OneToMany(targetEntity = BillOption.class, cascade = {CascadeType.PERSIST })
	@JoinColumn(name="_pa_shop_month_detail_id")
	public List<BillOption> getBillOptions() {
		return billOptions;
	}

	public void setBillOptions(List<BillOption> billOptions) {
		this.billOptions = billOptions;
	}

	@Column(name = "_area_totalamount")
	public double getAreaTotalAmount() {
		return areaTotalAmount;
	}

	public void setAreaTotalAmount(double areaTotalAmount) {
		this.areaTotalAmount = areaTotalAmount;
	}

	@Column(name = "_remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_pa_shop_detail_id") 
	public PerformanceAssignShopDetail getPerformanceAssignShopDetail() {
		return performanceAssignShopDetail;
	}

	public void setPerformanceAssignShopDetail(
			PerformanceAssignShopDetail performanceAssignShopDetail) {
		this.performanceAssignShopDetail = performanceAssignShopDetail;
	}
	
	@Column(name = "_monthDate", length = 7)
	public String getMonthDate() {
		return monthDate;
	}

	public void setMonthDate(String monthDate) {
		this.monthDate = monthDate;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)  
	@JoinColumn(name = "_pm_user_list_id", updatable= true) 
	public PmUserList getPmUserList() {
		return pmUserList;
	}

	public void setPmUserList(PmUserList pmUserList) {
		this.pmUserList = pmUserList;
	}

	@Column(name = "_curren_month_in_qty")
	public Integer getCurrenMonthInQty() {
		return currenMonthInQty;
	}

	public void setCurrenMonthInQty(Integer currenMonthInQty) {
		this.currenMonthInQty = currenMonthInQty;
	}

	@Column(name = "_pre_month_in_qty")
	public Integer getPreMonthInQty() {
		return preMonthInQty;
	}

	public void setPreMonthInQty(Integer preMonthInQty) {
		this.preMonthInQty = preMonthInQty;
	}

	@OneToMany(targetEntity = AssignUser.class, cascade = { CascadeType.ALL }, mappedBy = "paShopMonthDetail")
	public List<AssignUser> getAssignUsers() {
		return assignUsers;
	}

	public void setAssignUsers(List<AssignUser> assignUsers) {
		this.assignUsers = assignUsers;
	}

//	@ManyToMany(fetch = FetchType.LAZY)
//	@JoinTable(name = "rs_pm_user_performance_pm_shop_month_detail_", joinColumns = @JoinColumn(name = "_shopmonth_detail_id"), inverseJoinColumns = @JoinColumn(name = "_user_performance_id"))
//	public List<UserPerformance> getUserPerformances() {
//		return userPerformances;
//	}
//
//	public void setUserPerformances(List<UserPerformance> userPerformances) {
//		this.userPerformances = userPerformances;
//	}
	
	@Transient
	public String getAssignUserNames(){
		if(this.getAssignUsers() != null){
			List<AssignUser> users = new ArrayList<AssignUser>();
			for (Iterator<AssignUser> iterator = this.getAssignUsers().iterator(); iterator.hasNext();) {
				AssignUser assignUser = iterator.next();
				
				if(PostType.Shop.equals(assignUser.getPostType())){
					users.add(assignUser);
				}
			}
			return ReflectionUtils.convertElementPropertyToString(users, "user.realName", ",");
		}
		return "";
	}
	
	@Transient
	public double getAmountByFinancialId(Long financiaId){
		List<BillOption> options = this.getBillOptions();
		
		if(options != null && options.size() > 0){
			for (Iterator<BillOption> iterator = options.iterator(); iterator.hasNext();) {
				BillOption billOption =  iterator.next();
				if(financiaId.equals(billOption.getFinancial().getId())){
					return billOption.getAmount();
				}
			}
		}
		return 0;
	}
}
